#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tcpclient.h"
#include "QMessageBox"
#include "unistd.h"

class Sleeper : public QThread
{
public:
    static void msleep(unsigned long msecs)
    {
        QThread::msleep(msecs);
    }
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    width = 651; height = 320;
    particleXpos = width; particleYpos = height / 2;

    ui->graphicsView->setScene(new QGraphicsScene(0, 0, width, height, this));

    connect(ui->lunchButton, SIGNAL(clicked()), this, SLOT(Start()));

    QList<QNetworkInterface> hosts = QNetworkInterface::allInterfaces();

    for(int i = 0; i < hosts.count(); i++)
    {
        if(hosts[i].name() == "eth0")
        {
            /*for(int j = 0; j < hosts[i].allAddresses().count(); j++)
            {
                ui->ipAddrs_cbox->addItem(hosts[i].allAddresses()[j].toString());
            }*/
            ui->ip_addr_txtb->setText(hosts[0].allAddresses()[2].toString());
            ui->ip_addr_end_txt->setText(hosts[0].allAddresses()[2].toString());
        }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Start()
{
    int port = ui->port_txtbox->text().toInt();

    QString ip_start = ui->ip_addr_txtb->text();
    QString ip_end = ui->ip_addr_end_txt->text();
    int timeout = ui->timeout_txt->text().toInt();

    quint16 id = ui->partnr_spinbox->value();
    //ip_start = "192.168.216.194"; // moj lokalny
    client = new TcpClient(this, ip_start, ip_end, port, id, timeout);
    connect(client, SIGNAL(clientReady()), this, SLOT(updateParticleData()));
}

void MainWindow::lunchStream()
{
    quint16 id = ui->partnr_spinbox->value();
}

void MainWindow::updateParticleData()
{
    int mfield = ui->dialMField->value();
    int efield = ui->dialEField->value();

    int velocity = client->velocity;
    int electric = client->electric_field;
    int magnetic = client->magnetic_field;

    int dx = velocity + electric ;

     if(dx > 50)
         dx = 50;

     //ui->outputLine->setText("Velocity: " + QString::number(dx));
     int argx = 0;

     for(int i = 650; i > 0; i -= dx)
     {
         argx += 10;

         int mg = (int)(sin (argx * (3.1415)/180) * 300 / 2) + 150;
         int y = magnetic * 2;

        //if(mg == y && (i == (650 - i)))
          //  argx = 10;

         ui->graphicsView->scene()->clear();
         ui->graphicsView->scene()->addEllipse(i, y, 10, 10, QPen(), QBrush(Qt::red));
         ui->graphicsView->scene()->addEllipse(650 - i, mg, 10, 10, QPen(), QBrush(Qt::blue));

         QApplication::processEvents();
         Sleeper::msleep(50);
     }

     ui->graphicsView->scene()->clear();

     // wyslij czasteczke
     velocity = dx;
     client->sendParticle(velocity);
}

void MainWindow::mainPanelEnableChange()
{
    bool state = false;

    if(state == true)
    {
        ui->partnr_spinbox->setMinimum(0);
        ui->partnr_spinbox->setValue(0);
        ui->partnr_spinbox->setEnabled(false);
    }
    else
    {
        ui->partnr_spinbox->setMinimum(1);
        ui->partnr_spinbox->setValue(1);
        ui->partnr_spinbox->setEnabled(true);
    }
}
